//
//  AppController.m
//  iMine
//
//  Created by gaoxiang on 09-3-30.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GameEngine.h"
#import "PlayerRecord.h"
#import "ScoreClient.h"
#import "AppController.h"
#import "PreferencesController.h"
#import "TopListWindowController.h"

static AppController *_instance;

@implementation AppController

+ (AppController *)sharedAppController
{
	if (!_instance)
	{
		_instance = [[[self class] alloc] init];
	}
	return _instance;
}

- (id) init
{
	if (self != nil) {
		self = [super init];
		preferencesController = [[PreferencesController alloc] init];
		topListWindowController = [[TopListWindowController alloc] init];
		
		NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
		[notificationCenter addObserver:self selector:@selector(gameStatusChanged:) name:@"gameStatusChanged" object:nil];
	}
	return self;
}

- (IBAction)openPreferences:(id)sender
{
	[preferencesController showWindow:nil];
}

- (IBAction)openTopListWindow:(id)sender
{
	[topListWindowController showWindow:nil];
}

- (IBAction)winSheetSendOK:(id)sender;
{
	int level = [[GameEngine sharedGameEngine] level];
	int elapsedTime = [[GameEngine sharedGameEngine] elapseSecond];
	NSString *userName = [userNameField stringValue];
	
	// Return to normal event handling
	[NSApp endSheet:winSheetWindow];
	// Hide the sheet
	[winSheetWindow orderOut:sender];
	
	if (level == MINE_LEVEL1) {
		[[PlayerRecord instance] setScoreForBeginner:userName withScore:elapsedTime];
	}
	else if (level == MINE_LEVEL2) {
		[[PlayerRecord instance] setScoreForNovice:userName withScore:elapsedTime];
	}
	else if (level == MINE_LEVEL3) {
		[[PlayerRecord instance] setScoreForExpert:userName withScore:elapsedTime];
	}
	
	if ([uploadCheckButton state] == NSOnState)
	{
		[ScoreClient sendPlayerScoreToServer:userName playedLevel:level elapsedTime:elapsedTime];
	}
}

- (void)gameStatusChanged:(NSNotification *)notification
{
	NSNumber *status = (NSNumber *)[notification object];
	if ([status intValue] != Win) {
		return;
	}
	
	int level = [[GameEngine sharedGameEngine] level];
	int elapsedTime = [[GameEngine sharedGameEngine] getElapseSecond];
	if (level == MINE_LEVEL1)
	{
		int score = [[PlayerRecord instance] playerScoreForBeginner];
		if (elapsedTime >= score)
			return;
		[userNameField setStringValue:[[PlayerRecord instance] playerNameForBeginner]];
	}
	else if (level == MINE_LEVEL2) {
		int score = [[PlayerRecord instance] playerScoreForNovice];
		if (elapsedTime >= score)
			return;
		[userNameField setStringValue:[[PlayerRecord instance] playerNameForNovice]];
	}
	else if (level == MINE_LEVEL3) {
		int score = [[PlayerRecord instance] playerScoreForExpert];
		if (elapsedTime >= score)
			return;
		[userNameField setStringValue:[[PlayerRecord instance] playerNameForExpert]];
	}
	else
	{
		return;
	}
	
	[elapsedSecondField setIntValue:elapsedTime];
	
	[NSApp beginSheet:winSheetWindow
	   modalForWindow:mineWindow
		modalDelegate:nil
	   didEndSelector:NULL
		  contextInfo:NULL];
}

@end
